package com.yanqu.road.server.cmd.activity.deepfishing;

import com.yanqu.road.entity.activity.deepfishing.entity.DeepFishingBagItemData;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.activity.DeepFishingProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.deepseafishing.DeepFishingModule;
import com.yanqu.road.server.pb.DeepFishingPb;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.GameProtocol;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

@Cmd(code = GameProtocol.S_DEEP_FISHING_END_GAME, desc = "深海觅诊开始游戏")
public class DeepFishingSettleGameCmd extends AbstractCommand {

    private int activityId;
    private List<DeepFishingBagItemData> bagResultList;
    private Map<Integer,Integer> eventMap;
    private int deep;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        DeepFishingProto.deepFishingGameSettlementReqMsg deepFishingGameSettlementReqMsg = DeepFishingProto.deepFishingGameSettlementReqMsg.parseFrom(packet.getBytes());
        activityId = deepFishingGameSettlementReqMsg.getActivityId();
        bagResultList = new ArrayList<>();
        eventMap = new ConcurrentHashMap<>();
        List<DeepFishingProto.deepFishingItemData> fishItemDataList = deepFishingGameSettlementReqMsg.getFishItemDataList();
        for (DeepFishingProto.deepFishingItemData data :fishItemDataList){
            bagResultList.add(DeepFishingPb.parseMsgToDeepFishBagItem(data));
        }
        for (DeepFishingProto.deepFishingGameEventTrigger eventTrigger :deepFishingGameSettlementReqMsg.getEventTriggerList()){
            eventMap.put(eventTrigger.getEventId(),eventTrigger.getDeep());
        }


        deep = deepFishingGameSettlementReqMsg.getDeep();
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        ret = player.getModule(DeepFishingModule.class).fishSettlement(activityId,bagResultList,eventMap,deep);
    }

    @Override
    public void sendData(GamePlayer player) {
        if (ret!=0){
            DeepFishingProto.deepFishingGameSettlementRespMsg.Builder respBuilder = DeepFishingProto.deepFishingGameSettlementRespMsg.newBuilder();
            respBuilder.setRet(ret);
            player.sendPacket(ClientProtocol.U_DEEP_FISHING_END_GAME,respBuilder);
        }
    }
}


